import numpy
import math
import scipy
import scipy.io
import scipy.misc
import scipy.cluster
import scipy.cluster.vq
import matplotlib
import matplotlib.pyplot
import os
import IPython
import IPython.parallel
import itertools
import random
import sklearn
import sklearn.decomposition
import sklearn.manifold
import sklearn.cluster
import sklearn.feature_extraction
import sklearn.feature_extraction.text
base_path = "/u/mlrobert/code/local/2013_drawing_assistant/data/"
img_path = base_path+"renderings/bunny2/apparent_ridges.num_latitude_lines=20.num_longitude_lines=20/"
filter_path = base_path+"filters/gabor/num_thetas=08/"
local_feature_cluster_centroids_path = \
base_path+"local_feature_cluster_centroids/bunny2/"+ \
"apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08.galif.patch_width=15.num_samples=32.num_tiles=04.num_samples=1000000.k=5000/"
global_features_path = \
base_path+"global_features/bunny2/"+ \
"apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08.galif.patch_width=15.num_samples=32.num_tiles=04.num_samples=1000000.k=5000/"
img_names_exts = sorted(os.listdir(img_path))
local_feature_cluster_centroids_path_name_ext = local_feature_cluster_centroids_path+"local_feature_cluster_centroids.mat"
global_features_names_exts = sorted(os.listdir(global_features_path))
num_thetas = 8
num_images = len(img_names_exts)
num_thetas = 8
filter_path = base_path+"filters/gabor/num_thetas=08/"
tmp = []
for theta_index in range(num_thetas):
current_g_mat = scipy.io.loadmat(filter_path+"theta=%02d.mat" % theta_index)
current_g = current_g_mat["g"]
tmp.append(current_g)
g = numpy.array(tmp)
local_feature_cluster_centroids_mat = scipy.io.loadmat(local_feature_cluster_centroids_path_name_ext)
local_feature_cluster_centroids = local_feature_cluster_centroids_mat["local_feature_cluster_centroids"]
all_global_features = []
for global_features_name_ext in global_features_names_exts:
global_features_mat = scipy.io.loadmat(global_features_path+global_features_name_ext)
global_features = global_features_mat["global_features"].ravel()
all_global_features.append(global_features)
all_global_features_2d = numpy.array(all_global_features)
tf_idf_transformer = sklearn.feature_extraction.text.TfidfTransformer()
tf_idf_all_global_features_2d = numpy.array(tf_idf_transformer.fit_transform(all_global_features_2d).todense()).squeeze()
img_index = 124
img = scipy.misc.imread(img_path+img_names_exts[img_index]).astype(numpy.float32)[:,:,0]
img = (255.0 - img) / 255.0
imshow(img, cmap="gray");
colorbar();
#
# compute filter responses
#
filter_responses = []
for theta_index in range(num_thetas):
F_img = numpy.fft.fft2(img)
F_img_centered = numpy.fft.fftshift(F_img)
F_img_centered_filtered = F_img_centered * g[theta_index]
F_img_centered_filtered_uncentered = numpy.fft.ifftshift(F_img_centered_filtered)
filter_response = numpy.fft.ifft2(F_img_centered_filtered_uncentered)
filter_responses.append(filter_response)
filter_responses = numpy.array(filter_responses)
#
# compute local features
#
patch_width_as_fraction_of_width = 0.15
num_samples_width = 32
num_tiles_per_dimension_per_patch = 4
use_squared_filter_response = True
local_feature_norm_accept_threshold = 0.01
codebook_size = 5000
filter_response = filter_responses[0,:,:]
num_samples_height = (float(filter_response.shape[0])*float(num_samples_width))/float(filter_response.shape[1])
samples_y = numpy.linspace(0, filter_response.shape[0]-1, num_samples_height+2).astype(numpy.int32)
samples_x = numpy.linspace(0, filter_response.shape[1]-1, num_samples_width+2).astype(numpy.int32)
patch_size = int(filter_response.shape[1] * patch_width_as_fraction_of_width)
if patch_size % 2 == 0:
patch_size = patch_size + 1
patch_half_size = (patch_size - 1) / 2
all_local_features = []
for sample in list(itertools.product(samples_y, samples_x)):
local_features = []
for theta_index in range(num_thetas):
filter_response = filter_responses[theta_index,:,:]
if use_squared_filter_response:
filter_response_to_sample = abs(filter_response)*abs(filter_response)
else:
filter_response_to_sample = abs(filter_response)
filter_response_to_sample_padded = numpy.pad(filter_response_to_sample, (patch_half_size,patch_half_size), "constant")
patch = filter_response_to_sample_padded[sample[0]:sample[0]+patch_size, sample[1]:sample[1]+patch_size]
tile_bounds = numpy.linspace(0, patch_size, num_tiles_per_dimension_per_patch+1).astype(numpy.int32)
tile = numpy.zeros((num_tiles_per_dimension_per_patch, num_tiles_per_dimension_per_patch), dtype=numpy.float32)
for tile_index in list(itertools.product(range(num_tiles_per_dimension_per_patch), range(num_tiles_per_dimension_per_patch))):
tile[tile_index] = numpy.mean(patch[tile_bounds[tile_index[0]]:tile_bounds[tile_index[0]+1], \
tile_bounds[tile_index[1]]:tile_bounds[tile_index[1]+1]])
tile_1d = tile.ravel()
local_features.append(tile_1d)
local_features_1d = numpy.array(local_features).ravel()
if numpy.linalg.norm(local_features_1d) > local_feature_norm_accept_threshold:
all_local_features.append(local_features_1d)
#
# compute global features
#
all_local_features_2d = numpy.array(all_local_features)
all_local_features_2d_codebook_indices, distances = scipy.cluster.vq.vq(all_local_features_2d, local_feature_cluster_centroids)
hist, bin_edges = numpy.histogram(all_local_features_2d_codebook_indices, bins=codebook_size)
tf_idf_hist = numpy.array(tf_idf_transformer.transform(hist).todense()).squeeze()
print all_local_features_2d.shape
(601, 128)
plot(tf_idf_hist);
cos_distances = dot(tf_idf_all_global_features_2d, tf_idf_hist)
distances = arccos(cos_distances)
sorted_indices = argsort(distances)
print "%s (distance = %f) " % (img_names_exts[sorted_indices[0]], distances[sorted_indices[0]])
print "%s (distance = %f) " % (img_names_exts[sorted_indices[1]], distances[sorted_indices[1]])
print "%s (distance = %f) " % (img_names_exts[sorted_indices[2]], distances[sorted_indices[2]])
print "%s (distance = %f) " % (img_names_exts[sorted_indices[3]], distances[sorted_indices[3]])
print "%s (distance = %f) " % (img_names_exts[sorted_indices[4]], distances[sorted_indices[4]])
plot(distances);
latitude=07.longitude=04.png (distance = 0.000000) latitude=08.longitude=04.png (distance = 0.405799) latitude=09.longitude=04.png (distance = 0.451262) latitude=07.longitude=05.png (distance = 0.583955) latitude=09.longitude=05.png (distance = 0.627951)